package com.project.web.wechat.controller.app.cpWeChat;

import com.project.common.core.utils.exception.CustomException;
import com.project.common.core.utils.exception.Result;
import com.project.web.config.WxCorp.WxCpConfig;
import com.project.web.config.WxCorp.WxCpConfiguration;
import com.project.web.wechat.controller.common.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpMenuService;
import me.chanjar.weixin.cp.api.WxCpService;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 菜单管理
 *
 * @author wyy
 * @date 2020-06-24
 */
@Api(tags = "菜单管理接口")
@RestController("workWxMenuController")
@RequestMapping("/weChat/auth/workWx/menu")
public class WorkWxMenuController extends BaseController {

    /**
     * 创建菜单
     *
     * @return
     */
    @ApiOperation("创建菜单")
    @PostMapping(value = "/create")
    public Result<WxMenu> getAgentInfo(WxMenu wxMenu, HttpServletRequest request) {

        // 企业微信基本参数
        WxCpConfig wxCpConfig = getWxCpConfig(request);
        WxCpService wxCpService = WxCpConfiguration.getCpService(wxCpConfig.getCorpId(), wxCpConfig.getAgentId());
        WxCpMenuService wxCpMenuService = wxCpService.getMenuService();
        try {
            wxCpMenuService.create(Integer.valueOf(wxCpConfig.getAgentId()), wxMenu);
            return getResult(wxMenu);
        } catch (WxErrorException e) {
            log.error("获取应用信息出错：{}", ExceptionUtils.getStackTrace(e));
            throw new CustomException(String.valueOf(e.getError().getErrorCode()), e.getError().getErrorMsg());
        }
    }

    /**
     * 获取菜单
     *
     * @return
     */
    @ApiOperation("获取菜单")
    @GetMapping(value = "/get")
    public Result<WxMenu> getMenu(HttpServletRequest request) {

        // 企业微信基本参数
        WxCpConfig wxCpConfig = getWxCpConfig(request);
        WxCpService wxCpService = WxCpConfiguration.getCpService(wxCpConfig.getCorpId(), wxCpConfig.getAgentId());
        WxCpMenuService wxCpMenuService = wxCpService.getMenuService();
        try {
            Integer agentId = Integer.valueOf(wxCpConfig.getAgentId());
            WxMenu wxMenu = wxCpMenuService.get(agentId);
            return getResult(wxMenu);
        } catch (WxErrorException e) {
            log.error("获取应用信息出错：{}", ExceptionUtils.getStackTrace(e));
            throw new CustomException(String.valueOf(e.getError().getErrorCode()), e.getError().getErrorMsg());
        }
    }

    /**
     * 删除菜单
     *
     * @return
     */
    @ApiOperation("删除菜单")
    @GetMapping(value = "/delete")
    public Result<String> get(HttpServletRequest request) {
        // 企业微信基本参数
        WxCpConfig wxCpConfig = getWxCpConfig(request);
        WxCpService wxCpService = WxCpConfiguration.getCpService(wxCpConfig.getCorpId(), wxCpConfig.getAgentId());
        WxCpMenuService wxCpMenuService = wxCpService.getMenuService();
        try {
            Integer agentId = Integer.valueOf(wxCpConfig.getAgentId());
            wxCpMenuService.delete(agentId);
            return getResult(wxCpConfig.getAgentId());
        } catch (WxErrorException e) {
            log.error("获取应用信息出错：{}", ExceptionUtils.getStackTrace(e));
            throw new CustomException(String.valueOf(e.getError().getErrorCode()), e.getError().getErrorMsg());
        }
    }

}
